package com.qiall.bashutil;


import com.qiall.bashutil.event.CommandExecFailEvent;
import com.qiall.bashutil.event.CommandMessageEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class OsScriptUtil {



    public static String execScrip(String path) {
        String ret = "";
        try {
            Process proc = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", path});
            BufferedReader read = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            try {
                proc.waitFor();
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
            while (read.ready()) {
                System.out.println(read);
            }
            return ret;
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    public Runtime execCommand(Runtime runtime, String command, Integer outId) {
        if (runtime == null) {
            runtime = Runtime.getRuntime();
        }
        try {
            Process process = runtime.exec(new String[]{"/bin/sh", "-c", command});
            process.waitFor();
            BufferedReader read = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while (read.ready()) {
                String message = read.readLine();
                CommandMessageEvent commandMessageEvent = new CommandMessageEvent(this, message, outId);
              //  applicationContext.publishEvent(commandMessageEvent);
            }
        } catch (IOException | InterruptedException e) {
            CommandExecFailEvent commandExecFailEvent = new CommandExecFailEvent(this, e, outId);
          //  applicationContext.publishEvent(commandExecFailEvent);
            e.printStackTrace();
        }
        return runtime;
    }

    public static List<String> executeNewFlow(List<String> commands) {
        List<String> rspList = new ArrayList<String>();
        Runtime run = Runtime.getRuntime();
        try {
            Process proc = run.exec("/bin/bash", null, null);
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
            for (String line : commands) {
                out.println(line);
            }
            out.close();
            InputStream inputStream= proc.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null)
            {
                rspList.add(line);
            }
            reader.close();
            inputStream.close();
            proc.destroy();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return rspList;
    }

}
